﻿using Nest;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NestClient
{
    public class MyNestClient
    {
        public static readonly MyNestClient Intance = new MyNestClient();

        ElasticClient client;
        public MyNestClient()
        {
            var node = new Uri("http://127.0.0.1:9200");
            var settings = new ConnectionSettings(node);
            client = new ElasticClient(settings);
        }

        public object AddIndex<T>(string indexName) where T : class
        {
            if(client != null)
            {
                if (client.IndexExists(indexName).Exists == false)
                {
                    ICreateIndexResponse RS = client.CreateIndex(indexName, c => c.Mappings(ms => ms
                            .Map<T>(m => m.AutoMap())
                            )
                            );
                    return RS;
                }
            }
            return null;
        }

        public object DeleteIndex(string indexName)
        {
            if (client != null)
            {
                return client.DeleteIndex(indexName);
            }
            return null;
        }

        public object InsertDataToIndex<T>(T obj, string indexName) where T : class
        {
            if (client != null)
            {
                return client.Index(obj, idx => idx.Index(indexName));
            }
            return null;
        }

        public object DeleteDataFromIndex(object id, string indexName)
        {
            if (client != null)
            {
                return client.Delete<object>(id, d => d.Index(indexName));
            }
            return null;
        }

        public bool SearchDataExists<T>(T obj, string indexName) where T : class
        {
            return client.DocumentExists<T>(obj, d => d.Index(indexName)).Exists;
        }
        public List<T> SearchData<T>(Dictionary<string, object> parms) where T : class
        {
            var request = new SearchRequest
            {
                From = 0,
                Size = 1000,
                Query = new QueryContainer(),
            };

            foreach(var item in parms)
            {
                request.Query = request.Query ||
                    //new MatchPhraseQuery
                    //{
                    //    Field = item.Key,
                    //    Query = item.Value
                    //};
                    //new MatchQuery
                    //{
                    //    Field = item.Key,
                    //    Query = item.Value
                    //};
                    new TermQuery
                    {
                        Field = item.Key,
                        Value = item.Value
                    };
                    //new RegexpQuery { 
                    //    Field = item.Key, 
                    //    Value = item.Value, 
                    //    Flags = "INTERSECTION|COMPLEMENT|EMPTY",
                    //    Name = "named_query",
                    //    Boost = 1.1,
                    //};
            }


            return client.Search<T>(request).Documents.ToList();
        }
    }
}
